﻿using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace FLH.Regions
{
	/// <summary>
	/// RegionsSource maps to the Azure tables and provides the CRUD methods.
	/// </summary>
	public class RegionsSource
	{
		private readonly RegionsServiceContext _projectDataServiceContext;

		/// <summary>
		/// Constructs the datasource. Always checks to see if the table has
		/// previously been created. If not then it creates the table 
		/// using the data context. (NOT recommended for production code.)
		/// </summary>
		public RegionsSource()
		{
			var storageAccount = AzureTableInitialization.GetAccount();
			_projectDataServiceContext = new RegionsServiceContext(storageAccount.TableEndpoint.ToString(),
																				   storageAccount.Credentials);
			//Fixed in Azure				//{
			//    // Optimization: Where table name and class name are not the same
			//    // ResolveEntityType is the name of the method that supplies the object type
			//    // To improve performance of ADO.NET Data Service deserialization 
			//    ResolveType = (unused) => typeof(RegionsModel)
			//};

		}


		#region READ methods (by Partition, by Partition/Row) http://msdn.microsoft.com/en-us/library/dd894039.aspx

		/// <summary>
		/// Read (select) all rows in the specified partition from the client table.
		/// </summary>
		/// <param name="prefix">The prefix.</param>
		/// <returns></returns>
		public IEnumerable<RegionsModel> Read(string prefix)
		{
			if (String.IsNullOrEmpty(prefix)) return null;
			var partitionKey = prefix;

			if (String.IsNullOrEmpty(partitionKey)) return null;
			_projectDataServiceContext.MergeOption = MergeOption.NoTracking;	// set when you will not be updating this entity
			IQueryable<RegionsModel> results;
			try
			{
				results = from c in _projectDataServiceContext.RegionsTable
						  where c.PartitionKey == partitionKey
						  select c;
			}
			catch (Exception)
			{
				return null;
			}

			try
			{
				var query = results.AsTableServiceQuery();
				var queryResults = query.Execute();
				return queryResults;
			}
			catch (Exception)
			{
				return null;
			}
		}

		/// <summary>
		/// Read (select) the specified row in the specified partition from the client table.
		/// </summary>
		/// <param name="prefix">The prefix.</param>
		/// <param name="regionID">The region ID.</param>
		/// <returns></returns>
		public RegionsModel Read(string prefix, int regionID)
		{
			if (String.IsNullOrEmpty(prefix)) return null;
			var partitionKey = prefix;
			var rowKey = regionID.ToString();

			if (String.IsNullOrEmpty(partitionKey)) return null;
			_projectDataServiceContext.MergeOption = MergeOption.NoTracking;	// set when you will not be updating this entity
			RegionsModel results;
			try
			{
				results = (from c in _projectDataServiceContext.RegionsTable
						   where c.PartitionKey == partitionKey &&
								 c.RowKey == rowKey
						   select c).FirstOrDefault();
			}
			catch (Exception)
			{
				return null;
			}

			return results;
		}

		#endregion

	}
}


